package edu.hawaii.halealohacli.jcev.command;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.Calendar;
import java.util.Locale;
import org.junit.Test;
import org.wattdepot.client.WattDepotClient;

/**
 * Tests the CurrentPower class.
 * 
 * @author Eldon Visitacion
 */
public class TestCurrentPower {
  private String url = "http://server.wattdepot.org:8190/wattdepot/";
  private String tower = "Mokihana";
  private WattDepotClient sampleClient = new WattDepotClient(url);
  
  /**
   * testPower method.
   * @throws Exception If method call throws exception.
   */
  @Test
  public void testConnection() throws Exception {
    CurrentPower cp = new CurrentPower();
    assertNotNull("Ensures the connection is made.", cp);
  }
  
  /**
   * Test that the arguments are correct or not.
   * @throws Exception if name doesn't exist or improper format of arguments
   */
  @Test
  public void testArugments() throws Exception {
    CurrentPower de = new CurrentPower();
    de.execute(tower, sampleClient);
    assertTrue("True", de.testArgs());
    
    // more than 1 argument
    try {
      de.execute("Mokihana 2011-11-11", sampleClient);
    }
    catch (Exception e) {
      assertFalse("More than one argument given, should return false.", de.testArgs());
    }
    
    // argument with wrong name
    try {
      de.execute("Mokeyhans", sampleClient);
    }
    catch (Exception e) {
      assertFalse("Name doesn't exist, returns false", de.testArgs());
    }
    
    // No arguments
    try {
      de.execute("", sampleClient);
    }
    catch (Exception e) {
      assertFalse("No arguments, returns false", de.testArgs());
    }
  }
  
  /**
   * test that current time is accurate.
   * @throws Exception If method call throws exception.
   */
  @Test
  public void testCurrentTime() throws Exception {
    CurrentPower cp = new CurrentPower();
    cp.setAll(sampleClient, tower);
    long now = Calendar.getInstance(Locale.US).getTimeInMillis();
    long latency = now - cp.getTimeStamp().toGregorianCalendar().getTimeInMillis();
    System.out.println("Time differential: " + latency / 1000 + "s");
    assertTrue("Time should be accurate, within 5 mins is probably acceptable."
        , (latency / 1000) < 300);
  }
  
  /**
   * ensures we're getting the right tower.
   * @throws Exception If method call throws exception.
   */
  @Test
  public void testCorrectName() throws Exception {
    CurrentPower cp = new CurrentPower();
    cp.setAll(sampleClient, tower);
    System.out.println(cp.getName() + " " + tower);
    assertEquals("Ensures we are getting the correct tower.", cp.getName(), tower);
  }
  
  /**
   * ensures we're getting the right tower.
   * @throws Exception If method call throws exception.
   */
  @Test
  public void testPower() throws Exception {
    CurrentPower cp = new CurrentPower();
    cp.setAll(sampleClient, tower);
    System.out.println("Current Power reading in kW: " + cp.getPowerReading());
    assertTrue("Ensures we are getting a power reading.", cp.getPowerReading() > 0);
  }
}
